//Copyright (C) 2010 Ron Coleman. Contact: ronncoleman@gmail.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 3 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

/*
 * EditCommentDialog.java
 *
 * Created on Jan 23, 2010, 5:31:23 PM
 */

package org.workplicity.marist.grounds.ui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JTable;
import org.workplicity.entry.Comment;
import org.workplicity.entry.User;
import org.workplicity.entry.WorkRequest;
import org.workplicity.marist.grounds.ui.table.CommentsTableModel;
import org.workplicity.util.WorkDate;

/**
 * This class captures comments from the user.
 * @author Ron Coleman
 */
public class EditCommentDialog extends javax.swing.JDialog {
    private final User user;
    private final JTable table;
    private final CommentsTableModel model;
    private final WorkRequest request;
    private final boolean isNew;

    /** Creates new form EditCommentDialog */
    public EditCommentDialog(JDialog parent, User user, WorkRequest request, JTable table,boolean isNew) {
        super(parent, true);

        initComponents();

        this.setLocationRelativeTo(parent);

        this.table = table;

        this.model = (CommentsTableModel) table.getModel();

        this.user = user;

        this.request = request;

        this.isNew = isNew;

        initWithComment();

    }

    private void initWithComment() {
        // If this is a new comment, the defaults are fine
        if(isNew)
            return;

        // If comment time is AFTER the last update time or it is a new
        // request, the comment can be edited
        int row = table.getSelectedRow();

        Comment comment = model.getComments().get(row);

        this.commentTextArea.setText(comment.getText());

        if(request.getId() != -1 && comment.getDate().getTime() < request.getUpdateDate().getTime()) {
            this.saveButton.setEnabled(false);
            this.commentTextArea.setEnabled(false);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        commentTextArea = new javax.swing.JTextArea();
        cancelButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("Comment:");

        commentTextArea.setColumns(20);
        commentTextArea.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        commentTextArea.setRows(5);
        jScrollPane1.setViewportView(commentTextArea);

        cancelButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        saveButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        String text = this.commentTextArea.getText().trim();

        // If the user has typed something and it's a fresh comment
        // add it to the table
        if(text.length() != 0 && isNew) {
            List<Comment> comments = model.getComments();

            Comment comment = new Comment();

            comment.setText(text);

            comment.setUserId(user.getId());
            
            comments.add(comment);

            model.fireTableDataChanged();
        }
        // If the user has typed something and we're updating the
        // comment, then replace the comment in the table
        else if(text.length() != 0 && !isNew) {
            int row = table.getSelectedRow();

            List<Comment> comments = model.getComments();

            Comment comment = comments.get(row);

            comment.setText(text);

            comment.setUserId(user.getId());
            
            comment.setDate(new WorkDate(System.currentTimeMillis()));
            
            model.fireTableDataChanged();
        }
        // If the user has erased everything and we're updating the
        // comment, then delete the comment
        else if(text.length() == 0 && !isNew) {
            int row = table.getSelectedRow();

            List<Comment> comments = model.getComments();

            comments.remove(row);

            model.fireTableDataChanged();
        }

        dispose();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

//    /**
//    * @param args the command line arguments
//    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                EditCommentDialog dialog = new EditCommentDialog(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextArea commentTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
